package com.cf.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cf.domain.ResponseResult;
import com.cf.domain.pojo.Judges;
import com.cf.domain.pojo.News;
import com.cf.domain.pojo.PageQuery;
import com.cf.domain.vo.UserVO;
import com.cf.service.INewsService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author lzqinga@126.com
 * @Date 2024/11/14  9:16
 */
@Controller
@RequestMapping("/news")
@RequiredArgsConstructor
public class NewsController {

    private final INewsService newsService;

    @RequestMapping("/newsUI")
    public String newsUI(){
        return "admin/news";
    }

    /**
     * 添加
     */
    @PostMapping
    @ResponseBody
    public ResponseResult save(@RequestBody News news){
        newsService.save(news);
        return ResponseResult.sucess();
    }

    @PutMapping
    @ResponseBody
    public ResponseResult update(@RequestBody News news){
        newsService.updateById(news);
        return ResponseResult.sucess();
    }

    @GetMapping("/{id}")
    @ResponseBody
    public ResponseResult getById(@PathVariable Long id){
        News news = newsService.getById(id);
        return ResponseResult.sucess(news);
    }

    @GetMapping("/pagination")
    @ResponseBody
    public ResponseResult page(@RequestParam(defaultValue = "0") Integer pageNo,
                                 @RequestParam(defaultValue = "10") Integer pageSize,
                               @RequestParam(defaultValue = "0") Integer typeQuery){
        Map<String, Object> map = new HashMap<>();
        map.put("typeQuery", typeQuery);
        Page<News> data = newsService.page(new Page<News>(pageNo, pageSize), map);
        return ResponseResult.sucess(data);
    }

    @GetMapping("/index")
    @ResponseBody
    public ResponseResult index(){
        List<News> data = newsService.getAllFlags();
        return ResponseResult.sucess(data);
    }
}
